<template>
  <BasicModal v-bind="$attrs" @register="registerModal" title="日志详情" okText="复制" @ok="handleOK">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts">
import {defineComponent, ref, unref} from 'vue';
import {BasicModal, useModalInner} from '/@/components/Modal';
import {BasicForm, useForm} from '/@/components/Form/index';
import {logsFormSchema} from './logs.data';
import {useCopyToClipboard} from '/@/hooks/web/useCopyToClipboard';
import {useMessage} from '/@/hooks/web/useMessage';

export default defineComponent({
  name: 'AccountModal',
  components: {BasicModal, BasicForm},
  emits: ['success', 'register'],
  setup(_,) {
    const isUpdate = ref(true);
    const {createMessage} = useMessage();
    const {clipboardRef, copiedRef} = useCopyToClipboard();

    const [registerForm, {setFieldsValue, resetFields, validate}] = useForm({
      labelWidth: 100,
      baseColProps: {span: 24},
      schemas: logsFormSchema,
      showActionButtonGroup: false,
      actionColOptions: {
        span: 23,
      },
    });

    const [registerModal, {setModalProps}] = useModalInner(async (data) => {
      await resetFields();
      setModalProps({confirmLoading: false});
      isUpdate.value = !!data?.isUpdate;

      if (unref(isUpdate)) {
        await setFieldsValue({
          ...data.record,
        });
      }
    })

    const handleOK = async () => {
      const values = await validate();
      clipboardRef.value = JSON.stringify(unref(values));
      if (unref(copiedRef)) {
        createMessage.success('复制成功');
      }
    }
    return {registerModal, registerForm, handleOK};

  },
});
</script>
